package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.Textbook;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 教材dao类
 * Created by chenxin on 2014/12/10.
 */
@Repository
public class TextbookDao extends BaseDao {
    /**
     * 新增教材
     * @param textbook 教材实体类
     * @return boolean
     *
     * @author cx
     * @since 2014-12-10
     */
    public boolean save(Textbook textbook) {
        String sql = "INSERT INTO t_textbook VALUES(NULL,:title,:author,:publisher,:isbn,:courseId)";
        SqlParameterSource sps = new MapSqlParameterSource().addValue("title", textbook.getTitle())
                .addValue("author", textbook.getAuthor())
                .addValue("publisher", textbook.getPublisher())
                .addValue("isbn", textbook.getIsbn())
                .addValue("courseId", textbook.getCourseId());

        KeyHolder keyHolder = new GeneratedKeyHolder();

        int result = namedParameterJdbcTemplate.update(sql, sps, keyHolder);
        if (result == 1) {
            log.debug("新增教材成功：result == 1");
            //绑定新增教材的ID
            textbook.setId(keyHolder.getKey().longValue());
            return true;
        }

        log.debug("新增教材失败");
        return false;
    }

    /**
     * 修改教材
     * @param textbook 教材实体类
     * @return boolean
     *
     * @author cx
     * @since 2014-12-10
     */
    public boolean update(Textbook textbook) {
        String sql = "UPDATE t_textbook SET sTitle=:title,sAuthor=:author,sPublisher=:publisher,sISBN=:isbn WHERE id=:id";
        SqlParameterSource sps = new MapSqlParameterSource().addValue("title", textbook.getTitle())
                .addValue("author", textbook.getAuthor())
                .addValue("publisher", textbook.getPublisher())
                .addValue("isbn", textbook.getIsbn())
                .addValue("id", textbook.getId());

        log.debug("修改教材");
        return namedParameterJdbcTemplate.update(sql, sps) == 1;
    }

    /**
     * 删除教材
     * @param id 教材ID
     * @return boolean
     *
     * @author cx
     * @since 2014-12-10
     */
    public boolean delete(final long id) {
        String sql = "DELETE FROM t_textbook WHERE id=:id";

        log.debug("删除教材");
        return namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource().addValue("id", id)) == 1;
    }

    /**
     * 获取课程教材
     * @param courseId 课程ID
     * @return List
     *
     * @author cx
     * @since 2014-12-10
     */
    public List<Textbook> getAll(final long courseId) {
        String sql = "SELECT * FROM t_textbook WHERE iCourseId=:courseId";
        SqlParameterSource sps = new MapSqlParameterSource().addValue("courseId", courseId);

        log.debug("获取课程教材");
        return namedParameterJdbcTemplate.query(sql, sps, new RowMapper<Textbook>() {
            @Override
            public Textbook mapRow(ResultSet rs, int rowNum) throws SQLException {
                Textbook entity = new Textbook();

                entity.setId(rs.getLong("id"));
                entity.setTitle(rs.getString("sTitle"));
                entity.setAuthor(rs.getString("sAuthor"));
                entity.setPublisher(rs.getString("sPublisher"));
                entity.setIsbn(rs.getString("sISBN"));
                entity.setCourseId(rs.getLong("iCourseId"));

                return entity;
            }
        });
    }
}
